package com.handu.luna;

//import java.util.Arrays;
//import java.util.List;
//import java.util.function.Consumer;

import javax.servlet.Filter;
import javax.transaction.Transactional;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.filter.CharacterEncodingFilter;
//import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

import com.handu.luna.core.domain.support.EnumAwareConvertUtilsBean;
import com.handu.luna.mall.controller.support.UserInterceptor;

@Configuration
@EnableAutoConfiguration
@ComponentScan
@Transactional
@EnableJpaAuditing
@ImportResource("classpath:applicationContext.xml")
public class Application extends WebMvcConfigurerAdapter {

	/**
	 * 增加内置容器对utf-8系统的过滤器 如果没有这个过滤器，tomcat下回出现乱码，jetty没有太大问题
	 *
	 */
	@Configuration
	protected static class ApplicationContextFilterConfiguration {
		@Bean
		public Filter applicationContextIdFilter(ApplicationContext context) {
			CharacterEncodingFilter filter = new CharacterEncodingFilter();
			filter.setEncoding("UTF-8");
			return filter;
		}
	}

	/**
	 * spring mvc拦截器定义
	 * 
	 * @return 用于权限管理的拦截器
	 */
	@Bean
	public UserInterceptor userInterceptor() {
		return new UserInterceptor();
	}

	/**
	 * 为springboot增加spring mvc拦截器定义
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(userInterceptor()).excludePathPatterns("/images/*");
	}

	public static void main(String[] args) {
		BeanUtilsBean.setInstance(new BeanUtilsBean(
				new EnumAwareConvertUtilsBean()));
		SpringApplication app = new SpringApplication(Application.class);
		app.setShowBanner(true);
		app.run(args);
	}
}